/**
 * Sailorbase Dialog functions
 */

dojo.provide("sailorbase.Dialog");
dojo.declare("sailorbase.Dialog", [ dijit.Dialog ], {
	// summary:
	// extended version of the dojo Dialog widget with the option to disable
	// the close button and supress the escape key.
	disableCloseButton : true,
	/* *********************************************************** postCreate */
	postCreate : function() {
		this.inherited(arguments);
		this._updateCloseButtonState();
	},
	/* *************************************************************** _onKey */
	_onKey : function(evt) {
		if (this.disableCloseButton && evt.charOrCode == dojo.keys.ESCAPE)
			return;
		this.inherited(arguments);
	},
	/* ************************************************ setCloseButtonDisabled */
	setCloseButtonDisabled : function(flag) {
		this.disableCloseButton = flag;
		this._updateCloseButtonState();
	},
	/* ********************************************** _updateCloseButtonState */
	_updateCloseButtonState : function() {
		dojo.style(this.closeButtonNode, "display",
				this.disableCloseButton ? "none" : "block");
	}
});

/* Utility function to raise a dialog asking the user a yes/no question. */
function raiseConfirmationDialog(title, question, callbackFn) {
	var translate = dojo.i18n.getLocalization("sailorbase", "global");

	var confirmationDialog = new sailorbase.Dialog({
		id : 'confirmationDialog',
		title : title
	});
	// When either button is pressed, kill the dialog and call the callbackFn.
	var commonCallback = function(mouseEvent) {
		confirmationDialog.hide();
		confirmationDialog.destroyRecursive();
		var myBrowser = whichBrowser();
		var validateResult = false;
		if (myBrowser == 'Firefox')
			validateResult = (mouseEvent.explicitOriginalTarget.id == 'yesButton');
		else if (myBrowser == 'Explorer')
			validateResult = (mouseEvent.srcElement.id == 'yesButton');
		else if (myBrowser == 'Safari')
			validateResult = (mouseEvent.srcElement.id == 'yesButton_label');
		if (validateResult) {
			callbackFn();
		}
	};
	var questionDiv = dojo.create('div', {
		innerHTML : question
	});
	var yesButton = new dijit.form.Button({
		label : translate.global_yes,
		id : 'yesButton',
		onClick : commonCallback
	});
	var noButton = new dijit.form.Button({
		label : translate.global_no,
		id : 'noButton',
		onClick : commonCallback
	});

	confirmationDialog.containerNode.appendChild(questionDiv);
	confirmationDialog.containerNode.appendChild(yesButton.domNode);
	confirmationDialog.containerNode.appendChild(noButton.domNode);

	confirmationDialog.show();
}

/* Utility function to raise a dialog asking the user a add/remove question. */
function raiseAddRemoveDialog(title, question, callbackFn, parameterItem) {
	var translate = dojo.i18n.getLocalization("sailorbase", "global");

	var addRemoveDialog = new sailorbase.Dialog({
		id : 'addRemoveDialog',
		title : title
	});
	// When either button is pressed, kill the dialog and call the callbackFn.
	var commonCallback = function(mouseEvent) {
		addRemoveDialog.hide();
		addRemoveDialog.destroyRecursive();
		var myBrowser = whichBrowser();
		var validateResult = false;
		if (myBrowser == 'Firefox')
			validateResult = (mouseEvent.explicitOriginalTarget.id == 'addButton');
		else if (myBrowser == 'Explorer')
			validateResult = (mouseEvent.srcElement.id == 'addButton');
		else if (myBrowser == 'Safari')
			validateResult = (mouseEvent.srcElement.id == 'addButton_label');
		if (validateResult) {
			callbackFn('add', parameterItem);
		} else {
			callbackFn('remove', parameterItem);
		}
	};
	var questionDiv = dojo.create('div', {
		innerHTML : question
	});
	var addButton = new dijit.form.Button({
		label : translate.global_add,
		id : 'addButton',
		onClick : commonCallback
	});
	var removeButton = new dijit.form.Button({
		label : translate.global_remove,
		id : 'removeButton',
		onClick : commonCallback
	});

	addRemoveDialog.containerNode.appendChild(questionDiv);
	addRemoveDialog.containerNode.appendChild(addButton.domNode);
	addRemoveDialog.containerNode.appendChild(removeButton.domNode);

	addRemoveDialog.show();
}